SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
create FUNCTION [dbo].[FU_FECH_COBR]     
/*------------------------------------------------------*/          
/*---- Empresa           : OFISIS S.A.              ----*/          
/*---- Cliente           : OFISIS S.A.              ----*/          
/*---- Nombre FU         : FU_FECH_PAGO      ----*/          
/*---- Desarrollado por  : Daniel Chiang      ----*/          
/*---- Fecha Creación    : 09/03/2003               ----*/          
/*---- Base de Datos     : SLQ Server               ----*/          
/*---- Versión           : 8.0                      ----*/          
/*------------------------------------------------------*/          
/*---Modificado 1 por     : Miguel Carrasco         ----*/        
/*---Fecha Modificación   : 13/10/2008             ----*/        
/*---Detalle Modificación : fecha de Vencimiento   ----*/        
/*---o de Pago de Doc. Cliente                     ----*/        
/*------------------------------------------------------*/          
/*---  Declare  @PRUEBA TD_DT_001        
       Select @PRUEBA = (SELECT dbo.FU_FECH_COBR('01','0000000002','FAC', '0001-0000000047','08/01/2008'))        
       Select @PRUEBA       ---*/        
/*------------------------------------------------------*/          
(        
@ISCO_EMPR VarChar(2),        
@ISCO_CLIE VarChar(20),        
@ISTI_DOCU VarChar(3),        
@ISNU_DOCU VarChar(20),        
@IDFE_VENC Datetime        
)        
        
Returns  Datetime        
        
As        
Begin        
  Declare        
  @VDFE_OPER Datetime,        
  @VDFE_COBR Datetime        
         
        Select @VDFE_COBR = Null        
        
 Select @VDFE_OPER = T3.FE_OPER        
        From        
 (        
 Select MAX(T2.FE_OPER) FE_OPER        
 From   TDMOVI_BANC T1, TCMOVI_BANC T2        
 Where  T1.NU_DOCU_ENTI = @ISNU_DOCU         
 And	T1.TI_DOCU_ENTI = @ISTI_DOCU        
 AND    T1.CO_ENTI = @ISCO_CLIE        
 And    T1.TI_ENTI = 'C'        
 And    T1.CO_EMPR = @ISCO_EMPR        
 And    T2.CO_EMPR = T1.CO_EMPR          
 And    T2.CO_BANC = T1.CO_BANC          
 And    T2.CO_UNID_CONC  = T1.CO_UNID_CONC
 And    T2.NU_CNTA_BANC  = T1.NU_CNTA_BANC          
 And    T2.AA_BNCO = T1.AA_BNCO        
 And    T2.MM_BNCO = T1.MM_BNCO         
 And    T2.TI_MOVI_BANC = T1.TI_MOVI_BANC         
 And    T2.NU_COMP_BANC = T1.NU_COMP_BANC  
 And    T2.TI_SITU = 'ACT'  
 GROUP BY T1.TI_DOCU_ENTI,T1.NU_DOCU_ENTI        
 Union All        
 Select MAX (T2.FE_OPER)   
 From   TDMOVI_CAJA T1, TCMOVI_CAJA T2        
 Where  T1.NU_DOCU_ENTI = @ISNU_DOCU        
 AND    T1.TI_DOCU_ENTI = @ISTI_DOCU        
 AND    T1.CO_ENTI = @ISCO_CLIE        
 And    T1.TI_ENTI = 'C'        
 And    T1.CO_EMPR = @ISCO_EMPR        
 And    T2.CO_EMPR = T1.CO_EMPR          
 And    T2.CO_CAJA = T1.CO_CAJA        
 And    T2.AA_CAJA = T1.AA_CAJA        
 And    T2.MM_CAJA = T1.MM_CAJA         
 And    T2.TI_MOVI = T1.TI_MOVI        
 And    T2.NU_COMP_CAJA = T1.NU_COMP_CAJA  
 And    T2.TI_SITU = 'ACT'         
 GROUP BY T1.TI_DOCU_ENTI,T1.NU_DOCU_ENTI) T3   
         
        If @VDFE_OPER IS NOT NULL        
           If @VDFE_OPER < @IDFE_VENC        
              Select @VDFE_COBR = @VDFE_OPER        
           Else        
              Select @VDFE_COBR = @IDFE_VENC        
        Else        
              Select @VDFE_COBR = @IDFE_VENC         
        
Return @VDFE_COBR        
    
End        
    
/*------------------------ FIN ----------------------------*/      


GO
